<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\ReceiveAddrCreateRequest;
use App\Models\Province;
use App\Models\UserReceiveAddr;
use App\Transformer\AddrTransformer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AddrController extends Controller
{
    public function lists(Request $request)
    {
        $lists = UserReceiveAddr::where([['user_id',$request->get('user')->user_id],['status',1]])->get();
        foreach ($lists as $list){
            $list->province_name;
            $list->city_name;
            $list->area_name;
        }


        return ['status_code'=>1,'data'=>$lists];
    }

    public function create(ReceiveAddrCreateRequest $request)
    {
        $data = [
            'addr'=>$request->get('addr'),
            'province'=>$request->get('province'),
            'city'=>$request->get('city'),
            'area'=>$request->get('area'),
            'name'=>$request->get('name'),
            'is_default'=>$request->get('is_default',0),
            'phone'=>$request->get('phone'),
            'status'=>1,
            'user_id'=>$request->get('user')->user_id,
        ];
        if($request->get('is_default')==1){
            UserReceiveAddr::where('user_id',$request->get('user')->user_id)
                ->update([
                    'is_default'=>0
                ]);
        }
        return UserReceiveAddr::create($data)?
            ['status_code'=>1,'message'=>"success"]:
            ['status_code'=>0,'message'=>"fail"];
    }

    public function del(Request $request)
    {
        UserReceiveAddr::where([
            ['receive_addr_id',$request->get('receive_addr_id')],
            ['user_id',$request->get('user')->user_id]
        ])
            ->update(['status'=>0]);
    }

    public function edit(ReceiveAddrCreateRequest $request)
    {
        return UserReceiveAddr::where([
            ['receive_addr_id',$request->get('receive_addr_id')],
            ['user_id',$request->get('user')->user_id]
        ])->update([
            'province'=>$request->get('province'),
            'city'=>$request->get('city'),
            'area'=>$request->get('area'),
            'addr'=>$request->get('addr'),
            'name'=>$request->get('name'),
            'phone'=>$request->get('phone'),
        ])?
            ['status_code'=>1,'message'=>"success"]:
            ['status_code'=>0,'message'=>"fail"];

    }

    public function is_default(Request $request)
    {
        UserReceiveAddr::where('user_id',$request->get('user')->user_id)
            ->update([
               'is_default'=>0
            ]);
        $addr = UserReceiveAddr::find($request->get('receive_addr_id'));
        $addr->is_default=1;
        return $addr->save()?
            ['status_code'=>1,'message'=>"success"]:
            ['status_code'=>0,'message'=>"fail"];
    }

    public function all()
    {
        $provinces = Province::all();
        foreach ($provinces as $province){
            foreach ($province->my_city as $city){
                $city->my_area;
            };
        }
        return $provinces;
    }


}
